package com.xzy.sms.web.domain.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xzy.sms.web.domain.dao.ApplicationDao;
import com.xzy.sms.web.domain.entity.Application;
import com.xzyframework.persistence.DynamicSpecifications;
import com.xzyframework.persistence.SearchFilter;

/**
 * @desc 
 * @author wjw
 * @time 2017年11月16日下午3:33:35
 */
@Service
@Transactional
public class ApplicationService {
    
    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());
    
    /** 用户 */
    @Autowired
    private ApplicationDao applicationDao;
    
    /**
     * 根据id查找用户
     * 
     * @param id xx
     * @return xx
     */
    @Transactional(readOnly = true)
    public Application getOne(String id) {
        return applicationDao.getOne(id);
    }
    
    /**
     * 查询某一用户下的所有应用
     * @param userId xx
     * @param pageSize xx
     * @param page xx
     * @return xx
     */
    @Transactional(readOnly = true)
    public Page<Application> findByUserId(String userId, int page, int pageSize) {
    	
        Pageable pageable = new PageRequest(page, pageSize, new Sort(Direction.DESC, "applicationId"));
        return applicationDao.findByUserId(userId, pageable);
    }
    
    /**
     * 查询某一用户下的所有应用
     * @param userId xx
     * @return xx
     */
    @Transactional(readOnly = true)
    public List<Application> findByUserId(String userId) {
        return applicationDao.findByUserId(userId);
    }
    
    /**
     * FIXME 方法注释信息(此标记由Eclipse自动生成,请填写注释信息删除此标记)
     * @param searchParams x
     * @param pageNumber x
     * @param pageSize x
     * @return x
     */
    @Transactional(readOnly = true)
    public Page<Application> findAll(Map<String, Object> searchParams, int pageNumber, int pageSize) {
        
        PageRequest pageRequest = new PageRequest(pageNumber - 1, pageSize, new Sort(Direction.DESC, "applicationId"));
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        Specification<Application> spec = DynamicSpecifications.bySearchFilter(filters.values(), Application.class);
        return applicationDao.findAll(spec, pageRequest);
        
    }
    
    /**
     * FIXME 方法注释信息(此标记由Eclipse自动生成,请填写注释信息删除此标记)
     * @param searchParams x
     * @param pageNumber x
     * @param pageSize x
     * @return x
     */
    @Transactional(readOnly = true)
    public List<Application> findAll(Map<String, Object> searchParams) {
        
        Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
        Specification<Application> spec = DynamicSpecifications.bySearchFilter(filters.values(), Application.class);
        return applicationDao.findAll(spec);
        
    }
    
    /**
     * 查询某一用户下的所有应用
     * 
     * @param userId xx
     * @param pageSize xx
     * @param page xx
     * @return xx
     */
    @Transactional(readOnly = true)
    public Page<Application> findAll(int pageSize, int page) {
        Pageable pageable = new PageRequest(page, pageSize, new Sort(Direction.DESC, "applicationId"));
        return applicationDao.findAll(pageable);
    }
    
    /**
     * 保存实体
     * @param application xx
     * @return xx
     */
    public Application save(Application application) {
        Application newapp = applicationDao.save(application);
        return newapp;
    }
    
    /**
     * 逻辑删除
     * @param applicationId
     */
    public void delete(String applicationId) {
        applicationDao.setDelFlag(applicationId);
    }
    
}
